package com.xiang.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiang.reggie.common.CustomException;
import com.xiang.reggie.dao.CategoryMapper;
import com.xiang.reggie.entity.Category;
import com.xiang.reggie.entity.Dish;
import com.xiang.reggie.entity.Setmeal;
import com.xiang.reggie.service.CategoryService;
import com.xiang.reggie.service.DishService;
import com.xiang.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 半山居雾
 * @date 2022/12/15 11:12
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        //1.添加条件构造器，将id条件放入sql
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, id);

        //2.查询分类下关联的菜品数量
        int count = dishService.count(queryWrapper);
        //3.判断是否关联， 关联则抛出异常
        if (count > 0) {
            throw new CustomException("当前分类下关联了菜品，不能删除！");
        }

        //查询是否关联套餐
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(wrapper);
        if (count1 > 0) {
            throw new CustomException("当前分类下关联了套餐，不能删除！");
        }

        //正常执行删除
        super.removeById(id);

    }
}
